package com.origin.bluetooth

import android.bluetooth.BluetoothDevice
import android.bluetooth.le.ScanResult
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.databinding.ObservableField
import androidx.recyclerview.widget.LinearLayoutManager
import com.origin.baselibrary.activity.BaseActivity
import com.origin.baselibrary.activity.style.ImmersiveActivity
import com.origin.baselibrary.activity.style.ViewModelActivity
import com.origin.bluetooth.adapter.BlueToothAdapter
import com.origin.bluetooth.databinding.ActivityBlueToothTestBinding
import com.origin.uilibrary.xrecycleview.adapter.BaseRecycleAdapter

class BlueToothTestActivity : ViewModelActivity<ActivityBlueToothTestBinding, BlueToothViewModel>(R.layout.activity_blue_tooth_test) {
    private val blueToothAdapter: BlueToothAdapter by lazy {
        BlueToothAdapter(viewModel.bluetoothDeviceList, this);
    }
    private var describeString = ObservableField<String>()
    private var sendMsg = ObservableField<String>()

    override fun pageLogical() {
        describeString.set(
            "是否支持蓝牙4.0：" + viewModel.checkBle(this).toString() + " \n"
                    + "蓝牙是否开启：" + viewModel.isBlueToothEnable().toString()
        )
    }

//    override fun contentView(): Int = R.layout.activity_blue_tooth_test

    override fun initContentView() {
        contentViewBinding.describe = describeString;
        contentViewBinding.logInfo = viewModel.logInfo
        contentViewBinding.sendMsg = sendMsg

        contentViewBinding.blueToothService.setOnClickListener {
            viewModel.blueToothService(this)
            contentViewBinding.xrv.visibility = View.GONE
            contentViewBinding.pinBlueToothService.visibility = View.GONE
            contentViewBinding.blueToothService.visibility = View.GONE
        }

        contentViewBinding.pinBlueToothService.setOnClickListener {
            contentViewBinding.xrv.visibility = View.VISIBLE
            contentViewBinding.blueToothService.visibility = View.GONE
            contentViewBinding.pinBlueToothService.visibility = View.GONE
            viewModel.startDiscoveryDevice(this.application)
        }

        contentViewBinding.sendMsgBn.setOnClickListener {
            sendMsg.get()?.apply {
                viewModel.sendMsg(this)
            }
        }

        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
//        contentViewBinding.xrv.setPullRefreshEnabled(false)
        contentViewBinding.xrv.layoutManager = layoutManager
        contentViewBinding.xrv.adapter = blueToothAdapter
        viewModel.bluetoothDeviceList.mutableListState.observe(this, {
            blueToothAdapter.notifyDataSetChanged()
        })
        blueToothAdapter.mItemClickListener = object : BaseRecycleAdapter.OnItemClickListener<BluetoothDevice> {
            override fun onItemClick(position: Int, item:BluetoothDevice, view: View?) {
                viewModel.pin(this@BlueToothTestActivity, viewModel.bluetoothDeviceList[position])
            }
        }
    }
}